package app.beit.finportal.utils;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64EncoderStream;

public class FinportalEncryption {
	private static Cipher ecipher;
	private static Cipher dcipher;
	private static final int iterationCount = 10;
	private static byte[] salt = { (byte) 0xB2, (byte) 0x12, (byte) 0xD5,
			(byte) 0xB2, (byte) 0x44, (byte) 0x21, (byte) 0xC3, (byte) 0xC3 };
	private static FinportalEncryption instance = null;
	
//	public static void main(String[] args) throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException{
//		String pass = "p@ssw0rd";
//		FinportalEncryption f = FinportalEncryption.getInstance();
//		final String passEncryp = f.encrypt(pass);
//		System.err.println(passEncryp);
//		System.err.println(f.decrypt(passEncryp));		
//	}

	private FinportalEncryption() throws InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
		final String passPhrase = "Finportal Password Encryptions in NHSO";
		final KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
		final SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
		final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
		
		ecipher = Cipher.getInstance(key.getAlgorithm());
		dcipher = Cipher.getInstance(key.getAlgorithm());
		ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
		dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
	}
	
	public static FinportalEncryption getInstance() throws InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException{
		if(instance == null){
			instance = new FinportalEncryption();
		}
		
		return instance;
	}

	public String encrypt(String str) {
		try {
			byte[] utf8 = str.getBytes("UTF8");
			byte[] enc = ecipher.doFinal(utf8);
			enc = BASE64EncoderStream.encode(enc);
			return new String(enc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String decrypt(String str) {
		try {
			byte[] dec = BASE64DecoderStream.decode(str.getBytes());
			byte[] utf8 = dcipher.doFinal(dec);
			return new String(utf8, "UTF8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
